class AttachmentsController < ApplicationController
  before_filter :login_required, :authorization_required, :find_customer
  
  def index
    respond_to do |format|
      format.html {render :action => 'list.rhtml'}
      format.js {render :action => 'list.rjs'}
      format.xml {render :xml => @customer.attachments.to_xml}
    end
  end
  
  def new
    @attachment = Attachment.new
    respond_to do |format|
      format.html {render :action => 'new.rhtml'}
      format.js {render :action => 'new.rjs'}
    end
  end
  
  def create
    attachment = Attachment.new(params[:attachment])
    @customer.attachments << attachment
    if @customer.save
      responds_to_parent do
        render :action => 'list.rjs'
      end
    else
      render :action => 'new' unless request.xhr?
    end
    
  end
  
  def destroy
    Attachment.find(params[:id]).destroy
  end
  
  def destroy_selected
    unless params[:selected].nil?
      params[:selected].each do |id|
        attachment = Attachment.find(id)
        @customer = attachment.customer
        attachment.destroy
      end
    end
    redirect_to :controller => 'customers', :action => 'show', :id => @customer unless request.xhr?
  end
  
  def view
    attachment = Attachment.find(params[:id])
    send_file attachment.public_filename, :filename => attachment.filename, :type => attachment.content_type
  end
    
    
end
